package cn.kgc.controller;

import cn.kgc.common.Constants;
import cn.kgc.pojo.User;
import cn.kgc.service.UserService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("user")
@Data
public class UserController {
    @Resource
    UserService userService;

    @RequestMapping("login.action")
    public String login(HttpSession session, Model model, @RequestParam("username") String username, @RequestParam("password") String password) {
        System.out.println("login");
        User login = userService.login(username, password);
        if (login != null) {
            session.setAttribute("user", login);
            return "redirect:/user/query.action";
        } else {
            model.addAttribute("loginSuccess", "用户名或者密码错误");
            return "login";
        }
    }

    @RequestMapping("logout.action")
    public String logout(HttpSession session) {
        session.removeAttribute(Constants.USER_SESSION);
        return "login";
    }

    @RequestMapping("query.action")
    public String query(@RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                        @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                        @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                        HttpServletRequest request) {
        PageHelper.startPage(pageIndex, pageSize);
        List<User> userList = null;
        userList = userService.userList();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        request.setAttribute("pageInfo", pageInfo);
        return "userlist";
    }

    @RequestMapping(value = "/add.action", method = RequestMethod.GET)
    public String add(@ModelAttribute("user")User user) {
        return "register";
    }

    @RequestMapping(value = "/add.action", method = RequestMethod.POST)
    public String add(HttpServletRequest request, HttpServletResponse response, User user, @RequestParam(required = false, value = "pic_path") MultipartFile picpath) throws IOException {
        if (picpath != null) {
            if (picpath.getSize() > 1 * 1024 * 1024) {
                request.setAttribute("upoaderrormsg", "文件大小不能超过1M");
                return "register";
            }
            String realPath = request.getServletContext().getRealPath("images");
            String oldFileName = picpath.getOriginalFilename();
            String extension = FilenameUtils.getExtension(oldFileName);
            List<String> list = new ArrayList<>();
            list.add("jpg");
            list.add("png");
            list.add("jpeg");
            list.add("pneg");
            if (list.contains(extension) == false) {
                request.setAttribute("errormsg", "文件必须是图片");
                return "register";
            }
            String filename = System.currentTimeMillis() + RandomUtils.nextInt(1000000) + "_Personal.jpg";
            File file = new File(realPath, filename);
            try {
                picpath.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            user.setPicpath(filename);
            user.setSupper(1);
            int addUser = userService.insertUser(user);
            return "redirect: /user/query.action";
        }
        return "redirect: /user/query.action";

    }
    @RequestMapping("/delete.action")
    public String delete(@RequestParam("id") int id, HttpServletRequest request) {
        int delete = userService.deleteUser(id);
        if (delete > 0) {
            request.setAttribute("deleteMessage", "删除成功");
        } else {
            request.setAttribute("deleteMessage", "删除失败");
        }
        return "redirect: /user/query.action";
    }

}
